<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->db2 = Db::connect(config('database.db2'));
    }

    /**
     * 查看
     */
    public function index()
    {
        $city = input('city','');
        $date = input('date','');
        $salesman = input('salesman','');
        // 当前用户的权限
        if ($salesman) {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->order('group_id asc')->where(['aga.uid'=>$salesman])->find();
            $groups = [];
            if ($auth_group_access['group_id']==3) { // 业务员
                $groups[] = $salesman;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $salesman;
                $admin = $this->db2->name('admin')->where(['leader'=>$salesman])->find();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        } else {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->order('group_id asc')->where(['aga.uid'=>$this->auth->id])->find();
            $groups = [];
            if ($auth_group_access['group_id']==3) { // 业务员
                $groups[] = $this->auth->id;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $this->auth->id;
                $admin = $this->db2->name('admin')->where(['leader'=>$this->auth->id])->find();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        }
        $where_group= [];
        $where_group1= [];
        $where_group2= [];
        $where_group3= [];
        $where_group4= [];
        $where_group5= [];
        $where_group6= [];
        if ($groups) {
            $where_group['service_admin_id'] = ['in',$groups];
            $where_group1['p.service_admin_id'] = ['in',$groups];// 涉及到业务员交接，用活动的字段查询
            $where_group2['salesman_admin_id'] = ['in',$groups];
            $where_group3['s.salesman_admin_id'] = ['in',$groups];
            $where_group4['service_admin_id'] = ['in',$groups];
            $where_group5['po.service_admin_id'] = ['in',$groups];
            $where_group6['bl.service_admin_id'] = ['in',$groups];
        }
        $where_city = [];
        $where_city1 = [];
        if ($city) {
            $citys = explode('/', $city);
            if (count($citys)==3) {
                $end = end($citys);
                $where_city['area'] = $end;
                $where_city1['s.area'] = $end;
            } elseif (count($citys)==2) {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            } else {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            }
        }
        // 今日概况
        $time = time();
        $start_date = date('Y-m-d',$time);
        $end_date = date('Y-m-d H:i:s',$time);
        $start_time = strtotime($start_date);
        $end_time = $time;
        if ($date) {
            list($start_date,$end_date) = explode(' - ', $date);
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
        }
        $where1 = 'jointime>='.$start_time.' and jointime<'.$end_time;
        $where2 = 'create_dt>="'.$start_date.'" and create_dt<"'.$end_date.'"';
        $where3 = 's.create_dt>="'.$start_date.'" and s.create_dt<"'.$end_date.'"';
        $where4 = 'po.create_dt>="'.$start_date.'" and po.create_dt<"'.$end_date.'"';
        $where5 = 'u.jointime>='.$start_time.' and u.jointime<'.$end_time;
        $where6 = 'p.create_dt>="'.$start_date.'" and p.create_dt<"'.$end_date.'"';
        // 新增商家
        // $xzsj = $this->db2->name('shop')->where($where_city)->where($where_group2)->where(['shop_status'=>1])->where($where2)->count();
        // 新增有效店铺:有活动发布的店铺
        $xzyxdpdata = $this->db2->name('shop')->alias('s')->field('s.shop_no')->join('chbc_play p','p.shop_no=s.shop_no')->where(['s.shop_status'=>1,'p.play_status'=>['in','1,2,3,4,5']])->where($where_city1)->where($where_group1)->where($where3)->group('s.id')->select();
        $arr = [];
        foreach ($xzyxdpdata as $xz) {
            $arr[$xz['shop_no']] = $xz['shop_no'];
        }
        // 新增有效店铺
        $zxdpdata = $this->db2->name('shop')->alias('s')->field('s.shop_no')
            ->join('chbc_play p','p.shop_no=s.shop_no')
            ->where(['s.shop_status'=>1,'p.play_status'=>['in','1,2,3,4,5']])
            ->where($where_city1)->where($where_group1)
            ->where('p.start_date='.$start_time.' and p.end_date='.($start_time+86399))
            ->group('s.id')->select();
        $xzyxdpzx = 0;// 新增有效店铺中今天在线的数量
        foreach ($zxdpdata as $zx) {
            if (isset($arr[$zx['shop_no']])) $xzyxdpzx++;
        }
        $xzyxdp = count($xzyxdpdata);
        if ($xzyxdp>0) {
            $xzyxdp = $xzyxdpzx.'/'.$xzyxdp.'('.round(100*$xzyxdpzx/$xzyxdp,2).'%)';
        } else {
            $xzyxdp = $xzyxdpzx.'/'.$xzyxdp.'(0%)';
        }
        // 在线店铺：统计活动列表里 发过活动的店铺数
        $zxdps = $this->db2->name('shop')->alias('s')->field('s.shop_no')
            ->join('chbc_play p','p.shop_no=s.shop_no')
            // ->where(['s.shop_status'=>1,'p.play_status'=>['in','1,2,3,4,5']])
            ->where($where_city1)->where($where_group1)
            ->group('s.id')->select();
        $zxdp = count($zxdps);
        // 优质店铺:满返条件差价小于或等于3的店铺
        // $zxyzdp = $this->db2->name('shop')->alias('s')->join('chbc_play p','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('s.shop_status=1 and p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->group('s.id')->count();
        // 消耗名额
        $xhmedata = $this->db2->name('play_order')->alias('po')->join('chbc_shop s','po.shop_no=s.shop_no')->where('po.order_status in (3,6,-9) and po.is_effect=1')->where($where_city1)->where($where_group5)->where($where4)->field('po.order_status,po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2')->select();
        $xhme = 0;
        foreach ($xhmedata as $xh) {
            if (-1*$xh['service_charge_amt']!=$xh['actual_rebate_amt']) $xhme+=1;
        }
        // 总名额
        $zme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5)')->where($where6)->value('sum(p.num)');
        if ($zme==0) {
            $xhmezme=0;
        } else {
            $xhmezme = round(100*$xhme/$zme,2);
        }
        // 优质名额消耗
        // $yzmexh = (int)$this->db2->name('play')->alias('p')->join('chbc_play_order po','po.play_no=p.play_no')->join('chbc_shop s','po.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('po.order_status=3 and p.user_pay<=3+p.back_money')->where($where6)->count();
        // // 优质名额:满返条件差价小于或等于3的活动
        // $yzme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->value('sum(p.num)');
        // $yzmexhl = 0;
        // if ($yzme!=0) {
        //     $yzmexhl = round(100*$yzmexh/$yzme,2);
        // }
        // 平台服务费
        $ptfwf = $this->db2->name('play_order')
            ->alias('po')
            // ->join('chbc_play p','po.play_no=p.play_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-7,-8,-9']])->where($where_city1)->where($where_group5)->where($where4)->field('po.order_status,po.service_charge_amt,po.service_charge_amt2')->select();
        $xtlr = 0;
        foreach ($ptfwf as $pt) {
            if ($pt['order_status']==-8 || $pt['order_status']==-9) {
                $xtlr+=$pt['service_charge_amt2'];
            } else {
                $xtlr+=$pt['service_charge_amt'];
            }
        }
        $xtlr = round($xtlr,2);
        // 经纪人收益
        $middleman = $this->db2->name('middleman')->field('id,rebate_amt')->select();
        $rebate_amt = [];
        foreach ($middleman as $key => $value) {
            $rebate_amt[$value['id']] = $value['rebate_amt'];
        }
        $orders = $this->db2->name('play_order')
            ->alias('po')
            // ->join('chbc_user_money_log uml','uml.log_type=12 and po.play_order_no=uml.tran_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group5)->where('s.middleman_id>0')->where($where4)->field('s.service_amt,s.middleman_id')->select();
        $jjrsy = 0;
        if ($orders) {
            foreach ($orders as $key => $value) {
                if (isset($rebate_amt[$value['middleman_id']])) {
                    $jjrsy+=$rebate_amt[$value['middleman_id']];
                }
            }
        }
        $jjrsy = round($jjrsy,2);
        // 待支付金额
        $dzfjedata = $this->db2->name('play_order')
            ->alias('po')
            //->join('chbc_play p','po.play_no=p.play_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_pay'=>0,'po.is_effect'=>1])->where($where_city1)->where($where_group5)->where('po.create_dt<"'.$start_date.'"')->field('po.order_status,po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2')->select();
        $dzfje = 0;
        foreach ($dzfjedata as $dzf) {
            if ($dzf['order_status']==-8 || $dzf['order_status']==-9) {
                $dzfje+=$dzf['service_charge_amt2']+$dzf['actual_rebate_amt2'];
            } else {
                $dzfje+=$dzf['service_charge_amt']+$dzf['actual_rebate_amt'];
            }
        }
        $dzfje = round($dzfje,2);
        // $create_dt = $this->db2->name('bill_list')->order('id desc')->value('create_dt');
        // $dzfje = (float)$this->db2->name('bill_list')->alias('bl')
        //     ->join('chbc_shop s','bl.shop_no=s.shop_no')
        //     ->where(['bl.pay_status'=>'待支付'])->where($where_city1)->where($where_group6)->where('bl.create_dt="'.$create_dt.'"')->value('sum(bl.total_money)');
        $ptfwfdata = $this->db2->name('play_order')
            ->alias('po')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group5)->where($where4)->field('po.order_status,po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2')->select();
        $ptfwf = 0;
        foreach ($ptfwfdata as $xh) {
            if (-1*$xh['service_charge_amt']!=$xh['actual_rebate_amt']) $ptfwf+=1;
        }
        $ptfwf = round($ptfwf/2,1);
        // 业务收益
        $sjsy = round($xtlr-$jjrsy-$ptfwf,2);
        $jrgk = [
            // 'xzsj' => $xzsj,
            'xzyxdp' => $xzyxdp,
            'zxdp' => $zxdp,
            'xtlr' => $xtlr,
            // 'zxyzdp' => $zxyzdp,
            'mexh' => $xhme.'/'.$zme.' ('.$xhmezme.'%)',
            // 'yzmexhl' => $yzmexh.'/'.$yzme,
            'jjrsy' => $jjrsy,
            'sjsy' => $sjsy,
            'dzfje' => $dzfje,
            'ptfwf' => $ptfwf,
        ];
        $salesmanList = $this->salesmanList();
        // 当前用户所属用户组
        $auth_group_access = $this->db2->name('auth_group_access')->where(['uid'=>$this->auth->id])->order('group_id asc')->find();
        $this->view->assign("jrgk", $jrgk);
        $this->view->assign("salesmanList", $salesmanList);
        $this->view->assign("auth_group_access", $auth_group_access);
        // 是否是业务员
        $isywy = 0;
        $auth_group_access = Db::name('auth_group_access')->where('uid',$this->auth->id)->select();
        if (count($auth_group_access)==1 && $auth_group_access[0]['group_id']==3) {
            $isywy = 1;
        }
        // 销单
        $channels = $this->db2->name('channel')->where('status',1)->field('id,name')->select();
        $channels_group = [4=>[],5=>[],6=>[],7=>[]];
        $channelss = [];
        foreach ($channels as &$value) {
            $channelss[$value['id']] = $value['name'];
            if ($value['id']==3) {
                // 已销单
                $play_order = $this->db2->name('play_order')
                ->alias('po')
                ->join('chbc_shop s','po.shop_no=s.shop_no')
                ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'is_yunzhan'=>0])->where($where_city1)->where($where_group5)->where($where4)->field('po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2,po.order_status,po.is_yunzhan,po.service_admin_id')->select();
            } else {
                // 已销单
                $play_order = $this->db2->name('play_order')
                ->alias('po')
                ->join('chbc_shop s','po.shop_no=s.shop_no')
                ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'is_yunzhan'=>$value['id']])->where($where_city1)->where($where_group5)->where($where4)->field('po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2,po.order_status,po.is_yunzhan,po.service_admin_id')->select();
            }
            $dfje = 0;
            $dsje = 0;
            $yxd = 0;
            foreach ($play_order as $po) {
                if ($po['order_status']==-8 || $po['order_status']==-9) {
                    $dfje = $dfje+$po['actual_rebate_amt2'];
                    $dsje = $dsje+$po['service_charge_amt2']+$po['actual_rebate_amt2'];
                    $add1 = $po['actual_rebate_amt2'];
                    $add2 = $po['service_charge_amt2']+$po['actual_rebate_amt2'];
                } else {
                    $dfje = $dfje+$po['actual_rebate_amt'];
                    $dsje = $dsje+$po['service_charge_amt']+$po['actual_rebate_amt'];
                    $add1 = $po['actual_rebate_amt'];
                    $add2 = $po['service_charge_amt']+$po['actual_rebate_amt'];
                }
                $yxd++;
                if ($po['is_yunzhan']==0 || $isywy) continue;
                if (isset($channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dfje'])) {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dfje'] += $add1;
                } else {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dfje'] = $add1;
                }
                if (isset($channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dsje'])) {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dsje'] += $add2;
                } else {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dsje'] = $add2;
                }
                if (isset($channels_group[$po['is_yunzhan']][$po['service_admin_id']]['yxd'])) {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['yxd'] += 1;
                } else {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['yxd'] = 1;
                }
            }
            $dfje = round($dfje,2);
            $dsje = round($dsje,2);
            // 总量
            $quota = 0;
            if ($value['name']=='歪麦') {
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel,p.service_admin_id')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $add3 = $c['quota'];
                            $quota+=$c['quota'];break;
                        }
                    }
                    if ($isywy) continue;
                    if (isset($channels_group[$value['id']][$z['service_admin_id']]['quota'])) {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] += $add3;
                    } else {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] = $add3;
                    }
                }
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%歪卖%"')->where($where6)->field('p.channel,p.service_admin_id')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $add3 = $c['quota'];
                            $quota+=$c['quota'];break;
                        }
                    }
                    if ($isywy) continue;
                    if (isset($channels_group[$value['id']][$z['service_admin_id']]['quota'])) {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] += $add3;
                    } else {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] = $add3;
                    }
                }
            } else {
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel,p.service_admin_id')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $add3 = $c['quota'];
                            $quota+=$c['quota'];
                            break;
                        }
                    }
                    if ($value['id']==3 || $isywy) continue;
                    if (isset($channels_group[$value['id']][$z['service_admin_id']]['quota'])) {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] += $add3;
                    } else {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] = $add3;
                    }
                }
            }
            // 百分比
            if ($quota==0) {
                $xdbfb = 0;
            } else {
                $xdbfb = round(100*$yxd/$quota,2);
            }
            $value['yxd'] = $yxd;
            $value['zl'] = $quota;
            $value['xdbfb'] = $xdbfb;
            $value['dsje'] = $dsje; // 代收商家结算金额
            $value['dfje'] = $dfje; // 代付渠道结算金额
        }
        $this->view->assign("channels", $channels);
        // 非业务员计算渠道业务员结算
        $ywy = [];
        if ($isywy==0) {
            foreach ($channels_group as $key => $cg) {
                $auth_group_id = Db::name('auth_group')->where('name',$channelss[$key])->value('id');
                // 渠道的所有业务员
                $auth_group_user = Db::name('auth_group_access')->where('group_id',$auth_group_id)->select();
                foreach ($auth_group_user as $agu) {
                    $admin = Db::name('admin')->where('id',$agu['uid'])->find();
                    if (!$admin || !$admin['leader2']) continue;
                    $array = ['nickname'=>$admin['nickname'],'dfje'=>0,'dsje'=>0,'yxd'=>0,'zl'=>0,'xdbfb'=>0];
                    $sales_admin_id = Db::name('admin')->where('leader in ('.trim($admin['leader2'],',').')')->value('group_concat(id)');
                    if ($sales_admin_id) {
                        $sales_admin_id = explode(',', $sales_admin_id);
                        foreach ($sales_admin_id as $id) {
                            if (isset($cg[$id])) {
                                if (isset($cg[$id]['dfje'])) $array['dfje'] += $cg[$id]['dfje'];
                                if (isset($cg[$id]['dsje'])) $array['dsje'] += $cg[$id]['dsje'];
                                if (isset($cg[$id]['yxd'])) $array['yxd'] += $cg[$id]['yxd'];
                                if (isset($cg[$id]['quota'])) $array['zl'] += $cg[$id]['quota'];
                            }
                        }
                        // 百分比
                        if ($array['zl']==0) {
                            $array['xdbfb'] = 0;
                        } else {
                            $array['xdbfb'] = round(100*$array['yxd']/$array['zl'],2);
                        }
                    }
                    $array['dfje'] = round($array['dfje'],2);
                    $array['dsje'] = round($array['dsje'],2);
                    $ywy[] = $array;
                }
            }
        }
        $this->view->assign("ywy", $ywy);
        $this->view->assign("isywy", $isywy);
        return $this->view->fetch();
    }

    /**
     * 今日概况筛选
     * @return [type] [description]
     */
    public function dailyOverview()
    {
        $city = input('city','');
        $date = input('date','');
        $salesman = input('salesman','');
        // 当前用户的权限
        if ($salesman) {
            if ($salesman==17) {
                $groups[] = $salesman;
            } else {
                $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman])->find();
                $groups = [];
                if ($auth_group_access['group_id']==3) { // 业务员
                    $groups[] = $salesman;
                } elseif ($auth_group_access['group_id']==5) { // 经理
                    $groups[] = $salesman;
                    $admin = $this->db2->name('admin')->where(['leader'=>$salesman])->select();
                    foreach ($admin as $key => $value) {
                        $groups[] = $value['id'];
                    }
                }
            }
        } else {
            if ($salesman==17) {
                $groups[] = $salesman;
            } else {
                $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$this->auth->id])->find();
                $groups = [];
                if ($auth_group_access['group_id']==3) { // 业务员
                    $groups[] = $this->auth->id;
                } elseif ($auth_group_access['group_id']==5) { // 经理
                    $groups[] = $this->auth->id;
                    $admin = $this->db2->name('admin')->where(['leader'=>$this->auth->id])->select();
                    foreach ($admin as $key => $value) {
                        $groups[] = $value['id'];
                    }
                }
            }
        }
        $where_group= [];
        $where_group1= [];
        $where_group2= [];
        $where_group3= [];
        $where_group4= [];
        $where_group5= [];
        $where_group6= [];
        if ($groups) {
            $where_group['service_admin_id'] = ['in',$groups];
            $where_group1['p.service_admin_id'] = ['in',$groups];// 涉及到业务员交接，用活动的字段查询
            $where_group2['salesman_admin_id'] = ['in',$groups];
            $where_group3['s.salesman_admin_id'] = ['in',$groups];
            $where_group4['service_admin_id'] = ['in',$groups];
            $where_group5['po.service_admin_id'] = ['in',$groups];
            $where_group6['bl.service_admin_id'] = ['in',$groups];
        }
        $where_city = [];
        $where_city1 = [];
        if ($city) {
            $citys = explode('/', $city);
            if (count($citys)==3) {
                $end = end($citys);
                $where_city['area'] = $end;
                $where_city1['s.area'] = $end;
            } elseif (count($citys)==2) {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            } else {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            }
        }
        // 今日概况
        $time = time();
        $start_date = date('Y-m-d',$time);
        $end_date = date('Y-m-d H:i:s',$time);
        $start_time = strtotime($start_date);
        $end_time = $time;
        if ($date) {
            list($start_date,$end_date) = explode(' - ', $date);
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
        }
        $where1 = 'jointime>='.$start_time.' and jointime<'.$end_time;
        $where2 = 'create_dt>="'.$start_date.'" and create_dt<"'.$end_date.'"';
        $where3 = 's.create_dt>="'.$start_date.'" and s.create_dt<"'.$end_date.'"';
        $where4 = 'po.create_dt>="'.$start_date.'" and po.create_dt<"'.$end_date.'"';
        $where5 = 'u.jointime>='.$start_time.' and u.jointime<'.$end_time;
        $where6 = 'p.create_dt>="'.$start_date.'" and p.create_dt<"'.$end_date.'"';
        // 新增商家
        // $xzsj = $this->db2->name('shop')->where($where_city)->where($where_group2)->where(['shop_status'=>1])->where($where2)->count();
        // 新增有效店铺:有活动发布的店铺
        $xzyxdpdata = $this->db2->name('shop')->alias('s')->field('s.shop_no')->join('chbc_play p','p.shop_no=s.shop_no')->where(['s.shop_status'=>1,'p.play_status'=>['in','1,2,3,4,5']])->where($where_city1)->where($where_group1)->where($where3)->group('s.id')->select();
        $arr = [];
        foreach ($xzyxdpdata as $xz) {
            $arr[$xz['shop_no']] = $xz['shop_no'];
        }
        $todaytime = strtotime(date('Y-m-d'));
        $zxdpdata = $this->db2->name('shop')->alias('s')->field('s.shop_no')
            ->join('chbc_play p','p.shop_no=s.shop_no')
            ->where(['s.shop_status'=>1,'p.play_status'=>['in','1,2,3,4,5']])
            ->where($where_city1)->where($where_group1)
            ->where('p.start_date='.$todaytime.' and p.end_date='.($todaytime+86399))
            ->group('s.id')->select();
        $xzyxdpzx = 0;// 新增有效店铺中今天在线的数量
        foreach ($zxdpdata as $zx) {
            if (isset($arr[$zx['shop_no']])) $xzyxdpzx++;
        }
        $xzyxdp = count($xzyxdpdata);
        if ($xzyxdp>0) {
            $xzyxdp = $xzyxdpzx.'/'.$xzyxdp.'('.round(100*$xzyxdpzx/$xzyxdp,2).'%)';
        } else {
            $xzyxdp = $xzyxdpzx.'/'.$xzyxdp.'(0%)';
        }
        // 在线店铺：统计活动列表里 发过活动的店铺数
        $zxdps = $this->db2->name('shop')->alias('s')->field('s.shop_no')
            ->join('chbc_play p','p.shop_no=s.shop_no')
            // ->where(['s.shop_status'=>1,'p.play_status'=>['in','1,2,3,4,5']])
            ->where($where_city1)->where($where_group1)
            ->where('p.start_date>='.$start_time.' and p.start_date<='.$end_time)
            ->group('s.id')->select();
        $zxdp = count($zxdps);
        // 优质店铺:满返条件差价小于或等于3的店铺
        // $zxyzdp = $this->db2->name('shop')->alias('s')->join('chbc_play p','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('s.shop_status=1 and p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->group('s.id')->count();
        // 消耗名额
        $xhmedata = $this->db2->name('play_order')->alias('po')->join('chbc_shop s','po.shop_no=s.shop_no')->where('po.order_status in (3,6,-9) and po.is_effect=1')->where($where_city1)->where($where_group5)->where($where4)->field('po.order_status,po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2')->select();
        $xhme = 0;
        foreach ($xhmedata as $xh) {
            if (-1*$xh['service_charge_amt']!=$xh['actual_rebate_amt']) $xhme+=1;
        }
        // 总名额
        $zme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5)')->where($where6)->value('sum(p.num)');
        if ($zme==0) {
            $xhmezme=0;
        } else {
            $xhmezme = round(100*$xhme/$zme,2);
        }
        // 优质名额消耗
        // $yzmexh = (int)$this->db2->name('play')->alias('p')->join('chbc_play_order po','po.play_no=p.play_no')->join('chbc_shop s','po.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('po.order_status=3 and p.user_pay<=3+p.back_money')->where($where6)->count();
        // 优质名额:满返条件差价小于或等于3的活动
        // $yzme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->value('sum(p.num)');
        // $yzmexhl = 0;
        // if ($yzme!=0) {
        //     $yzmexhl = round(100*$yzmexh/$yzme,2);
        // }
        // 平台服务费
        $ptfwf = $this->db2->name('play_order')
            ->alias('po')
            //->join('chbc_play p','po.play_no=p.play_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-7,-8,-9']])->where($where_city1)->where($where_group5)->where($where4)->field('po.order_status,po.service_charge_amt,po.service_charge_amt2')->select();
        $xtlr = 0;
        foreach ($ptfwf as $pt) {
            if ($pt['order_status']==-8 || $pt['order_status']==-9) {
                $xtlr+=$pt['service_charge_amt2'];
            } else {
                $xtlr+=$pt['service_charge_amt'];
            }
        }
        $xtlr = round($xtlr,2);
        $middleman = $this->db2->name('middleman')->field('id,rebate_amt')->where($where_group2)->select();
        $rebate_amt = [];
        foreach ($middleman as $key => $value) {
            $rebate_amt[$value['id']] = $value['rebate_amt'];
        }
        $orders = $this->db2->name('play_order')
            ->alias('po')
            // ->join('chbc_user_money_log uml','uml.log_type=12 and po.play_order_no=uml.tran_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group5)->where('s.middleman_id>0')->where($where4)->field('s.service_amt,s.middleman_id')->select();
        $jjrsy = 0;
        if ($orders) {
            foreach ($orders as $key => $value) {
                if (isset($rebate_amt[$value['middleman_id']])) {
                    $jjrsy+=$rebate_amt[$value['middleman_id']];
                }
            }
        }
        $jjrsy = round($jjrsy,2);
        // 待支付金额
        $dzfjedata = $this->db2->name('play_order')
            ->alias('po')
            //->join('chbc_play p','po.play_no=p.play_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-9'],'po.is_pay'=>0])->where($where_city1)->where($where_group5)->where('po.create_dt<"'.date('Y-m-d',$time).'"')->field('po.order_status,po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2')->select();
        $dzfje = 0;
        foreach ($dzfjedata as $dzf) {
            if ($dzf['order_status']==-8 || $dzf['order_status']==-9) {
                $dzfje+=$dzf['service_charge_amt2']+$dzf['actual_rebate_amt2'];
            } else {
                $dzfje+=$dzf['service_charge_amt']+$dzf['actual_rebate_amt'];
            }
        }
        $dzfje = round($dzfje,2);
        // $create_dt = $this->db2->name('bill_list')->order('id desc')->value('create_dt');
        // $dzfje = (float)$this->db2->name('bill_list')->alias('bl')
        //     ->join('chbc_shop s','bl.shop_no=s.shop_no')
        //     ->where(['bl.pay_status'=>'待支付'])->where($where_city1)->where($where_group6)->where('bl.create_dt="'.$create_dt.'"')->value('sum(bl.total_money)');
        // 平台服务费
        $ptfwfdata = $this->db2->name('play_order')
            ->alias('po')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group5)->where($where4)->field('po.order_status,po.service_charge_amt,po.actual_rebate_amt,po.service_charge_amt2,po.actual_rebate_amt2')->select();
        $ptfwf = 0;
        foreach ($ptfwfdata as $xh) {
            if (-1*$xh['service_charge_amt']!=$xh['actual_rebate_amt']) $ptfwf+=1;
        }
        $ptfwf = round($ptfwf/2,1);
        // 业务收益
        $sjsy = round($xtlr-$jjrsy-$ptfwf,2);

        $jrgk = [
            // 'xzsj' => $xzsj,
            'xzyxdp' => $xzyxdp,
            'zxdp' => $zxdp,
            'xtlr' => $xtlr,
            // 'zxyzdp' => $zxyzdp,
            'mexh' => $xhme.'/'.$zme.' ('.$xhmezme.'%)',
            // 'yzmexhl' => $yzmexh.'/'.$yzme,
            'jjrsy' => $jjrsy,
            'sjsy' => $sjsy,
            'dzfje' => $dzfje,
            'ptfwf' => $ptfwf,
        ];
        $this->success('今日概况','',$jrgk);
    }

    /**
     * 销单
     * @return [type] [description]
     */
    public function salesOrder()
    {
        $city = input('city','');
        $date = input('date','');
        $salesman = input('salesman','');
        // 当前用户的权限
        if ($salesman) {
            if ($salesman==17) {
                $groups[] = $salesman;
            } else {
                $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman])->find();
                $groups = [];
                if ($auth_group_access['group_id']==3 || $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman,'aga.group_id'=>3])->find()) { // 业务员
                    $groups[] = $salesman;
                } elseif ($auth_group_access['group_id']==5) { // 经理
                    $groups[] = $salesman;
                    $admin = $this->db2->name('admin')->where(['leader'=>$salesman])->select();
                    foreach ($admin as $key => $value) {
                        $groups[] = $value['id'];
                    }
                }
            }
        } else {
            if ($salesman==17) {
                $groups[] = $salesman;
            } else {
                $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$this->auth->id])->find();
                $groups = [];
                if ($auth_group_access['group_id']==3 || $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman,'aga.group_id'=>3])->find()) { // 业务员
                    $groups[] = $this->auth->id;
                } elseif ($auth_group_access['group_id']==5) { // 经理
                    $groups[] = $this->auth->id;
                    $admin = $this->db2->name('admin')->where(['leader'=>$this->auth->id])->select();
                    foreach ($admin as $key => $value) {
                        $groups[] = $value['id'];
                    }
                }
            }
        }
        $where_group= [];
        $where_group1= [];
        $where_group2= [];
        $where_group3= [];
        $where_group4= [];
        $where_group5= [];
        if ($groups) {
            $where_group['service_admin_id'] = ['in',$groups];
            $where_group1['p.service_admin_id'] = ['in',$groups];// 涉及到业务员交接，用活动的字段查询
            $where_group2['salesman_admin_id'] = ['in',$groups];
            $where_group3['s.salesman_admin_id'] = ['in',$groups];
            $where_group4['service_admin_id'] = ['in',$groups];
            $where_group5['po.service_admin_id'] = ['in',$groups];
        }
        $where_city = [];
        $where_city1 = [];
        if ($city) {
            $citys = explode('/', $city);
            if (count($citys)==3) {
                $end = end($citys);
                $where_city['area'] = $end;
                $where_city1['s.area'] = $end;
            } elseif (count($citys)==2) {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            } else {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            }
        }
        // 今日概况
        $time = time();
        $start_date = date('Y-m-d',$time);
        $end_date = date('Y-m-d H:i:s',$time);
        $start_time = strtotime($start_date);
        $end_time = $time;
        if ($date) {
            list($start_date,$end_date) = explode(' - ', $date);
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
        }
        $where1 = 'jointime>='.$start_time.' and jointime<'.$end_time;
        $where2 = 'create_dt>="'.$start_date.'" and create_dt<"'.$end_date.'"';
        $where3 = 's.create_dt>="'.$start_date.'" and s.create_dt<"'.$end_date.'"';
        $where4 = 'po.create_dt>="'.$start_date.'" and po.create_dt<"'.$end_date.'"';
        $where5 = 'u.jointime>='.$start_time.' and u.jointime<'.$end_time;
        $where6 = 'p.create_dt>="'.$start_date.'" and p.create_dt<"'.$end_date.'"';
        // 是否是业务员
        $isywy = 0;
        $auth_group_access = Db::name('auth_group_access')->where('uid',$this->auth->id)->select();
        if (count($auth_group_access)==1 && $auth_group_access[0]['group_id']==3) {
            $isywy = 1;
        }
        // 销单
        $channels = $this->db2->name('channel')->where('status',1)->field('id,name')->select();
        $channels_group = [4=>[],5=>[],6=>[],7=>[]];
        $channelss = [];
        foreach ($channels as $key => &$value) {
            $channelss[$value['id']] = $value['name'];
            if ($value['id']==3) {
                // 已销单
                $play_order = $this->db2->name('play_order')
                ->alias('po')
                ->join('chbc_shop s','po.shop_no=s.shop_no')
                ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'is_yunzhan'=>0])->where($where_city1)->where($where_group5)->where($where4)->field('po.actual_rebate_amt,po.service_charge_amt,po.actual_rebate_amt2,po.service_charge_amt2,po.order_status,po.is_yunzhan,po.service_admin_id')->select();
            } else {
                // 已销单
                $play_order = $this->db2->name('play_order')
                ->alias('po')
                ->join('chbc_shop s','po.shop_no=s.shop_no')
                ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'is_yunzhan'=>$value['id']])->where($where_city1)->where($where_group5)->where($where4)->field('po.actual_rebate_amt,po.service_charge_amt,po.actual_rebate_amt2,po.service_charge_amt2,po.order_status,po.is_yunzhan,po.service_admin_id')->select();
            }
            $dfje = 0;
            $dsje = 0;
            $yxd = 0;
            foreach ($play_order as $po) {
                if ($po['order_status']==-8 || $po['order_status']==-9) {
                    $dfje = $dfje+$po['actual_rebate_amt2'];
                    $dsje = $dsje+$po['service_charge_amt2']+$po['actual_rebate_amt2'];
                    $add1 = $po['actual_rebate_amt2'];
                    $add2 = $po['service_charge_amt2']+$po['actual_rebate_amt2'];
                } else {
                    $dfje = $dfje+$po['actual_rebate_amt'];
                    $dsje = $dsje+$po['service_charge_amt']+$po['actual_rebate_amt'];
                    $add1 = $po['actual_rebate_amt'];
                    $add2 = $po['service_charge_amt']+$po['actual_rebate_amt'];
                }
                $yxd++;
                if ($po['is_yunzhan']==0 || $isywy) continue;
                if (isset($channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dfje'])) {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dfje'] += $add1;
                } else {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dfje'] = $add1;
                }
                if (isset($channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dsje'])) {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dsje'] += $add2;
                } else {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['dsje'] = $add2;
                }
                if (isset($channels_group[$po['is_yunzhan']][$po['service_admin_id']]['yxd'])) {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['yxd'] += 1;
                } else {
                    $channels_group[$po['is_yunzhan']][$po['service_admin_id']]['yxd'] = 1;
                }
            }
            $dfje = round($dfje,2);
            $dsje = round($dsje,2);
            // 总量
            $quota = 0;
            if ($value['name']=='歪麦') {
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel,p.service_admin_id')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $add3 = $c['quota'];
                            $quota+=$c['quota'];break;
                        }
                    }
                    if ($isywy) continue;
                    if (isset($channels_group[$value['id']][$z['service_admin_id']]['quota'])) {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] += $add3;
                    } else {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] = $add3;
                    }
                }
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%歪卖%"')->where($where6)->field('p.channel,p.service_admin_id')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $add3 = $c['quota'];
                            $quota+=$c['quota'];break;
                        }
                    }
                    if ($isywy) continue;
                    if (isset($channels_group[$value['id']][$z['service_admin_id']]['quota'])) {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] += $add3;
                    } else {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] = $add3;
                    }
                }
            } else {
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel,p.service_admin_id')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $add3 = $c['quota'];
                            $quota+=$c['quota'];
                            break;
                        }
                    }
                    if ($value['id']==3 || $isywy) continue;
                    if (isset($channels_group[$value['id']][$z['service_admin_id']]['quota'])) {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] += $add3;
                    } else {
                        $channels_group[$value['id']][$z['service_admin_id']]['quota'] = $add3;
                    }
                }
            }
            // 百分比
            if ($quota==0) {
                $xdbfb = 0;
            } else {
                $xdbfb = round(100*$yxd/$quota,2);
            }
            $value['yxd'] = $yxd;
            $value['zl'] = $quota;
            $value['xdbfb'] = $xdbfb;
            $value['dfje'] = $dfje;
            $value['dsje'] = $dsje;
        }
        // 非业务员计算渠道业务员结算
        $ywy = [];
        if ($isywy==0) {
            foreach ($channels_group as $key => $cg) {
                $auth_group_id = Db::name('auth_group')->where('name',$channelss[$key])->value('id');
                // 渠道的所有业务员
                $auth_group_user = Db::name('auth_group_access')->where('group_id',$auth_group_id)->select();
                foreach ($auth_group_user as $agu) {
                    $admin = Db::name('admin')->where('id',$agu['uid'])->find();
                    if (!$admin || !$admin['leader2']) continue;
                    $array = ['nickname'=>$admin['nickname'],'dfje'=>0,'dsje'=>0,'yxd'=>0,'zl'=>0,'xdbfb'=>0];
                    $sales_admin_id = Db::name('admin')->where('leader in ('.trim($admin['leader2'],',').')')->value('group_concat(id)');
                    if ($sales_admin_id) {
                        $sales_admin_id = explode(',', $sales_admin_id);
                        foreach ($sales_admin_id as $id) {
                            if (isset($cg[$id])) {
                                if (isset($cg[$id]['dfje'])) $array['dfje'] += $cg[$id]['dfje'];
                                if (isset($cg[$id]['dsje'])) $array['dsje'] += $cg[$id]['dsje'];
                                if (isset($cg[$id]['yxd'])) $array['yxd'] += $cg[$id]['yxd'];
                                if (isset($cg[$id]['quota'])) $array['zl'] += $cg[$id]['quota'];
                            }
                        }
                        // 百分比
                        if ($array['zl']==0) {
                            $array['xdbfb'] = 0;
                        } else {
                            $array['xdbfb'] = round(100*$array['yxd']/$array['zl'],2);
                        }
                    }
                    $array['dfje'] = round($array['dfje'],2);
                    $array['dsje'] = round($array['dsje'],2);
                    $ywy[] = $array;
                }
            }
        }
        $this->success('今日概况','',['channels'=>$channels,'ywy'=>$ywy]);
    }
}
